package sctu.edu.java.year2016.team01.chenhong.day20160616.chapter16_1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * Created by Administrator on 2016/6/16.
 */
public class MyClien extends JFrame {
    private PrintWriter writer;
    Socket socket;

    private  JTextArea ta=new JTextArea();
    private JTextField tf=new JTextField();
    Container cc;

    public MyClien(String title){
        super(title);
        cc=this.getContentPane();
        cc.add(ta,"North");
        cc.add(tf, "South");

        tf.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                writer.println(tf.getText());
                ta.append(tf.getText() + "\n");
                tf.setText("");
            }
        });}





        private void connnect(){
            ta.append("尝试连接\n");
            try {
                socket=new Socket("192.168.1.131",8998);
                writer=new PrintWriter(socket.getOutputStream(),true);
                ta.append("完成连接\n");
                InetAddress inetAddress=socket.getInetAddress();
                String netlp=inetAddress.getHostAddress();
                int netPort=socket.getLocalPort();
                InetAddress localAddress=socket.getLocalAddress();
                String locallp=localAddress.getHostAddress();
                int localPort=socket.getLocalPort();
                ta.append("远程服务器的IP地址:"+netlp+"\n");
                ta.append("远程服务器的端口号:"+netlp+"\n");
                ta.append("客户机本地的IP地址:"+netlp+"\n");
                ta.append("客户机本地的端口号:"+netlp+"\n");

            } catch (IOException e) {
                e.printStackTrace();
            }
        }








    public static void main(String[] args) {
        MyClien clien=new MyClien("向服务器送数据");
        clien.setBounds(300,260,340,220);
        clien.setVisible(true);
        clien.connnect();
    }






    }






















